Skip to content

Add unit tests for tidy parameter selection helpers#493

Merged
jgabry merged 3 commits intostan-dev:masterfrom
utkarshpawade:fix/add-tidy-params-unit-tests
Mar 18, 2026
Merged

Add unit tests for tidy parameter selection helpers#493
jgabry merged 3 commits intostan-dev:masterfrom
utkarshpawade:fix/add-tidy-params-unit-tests

Conversation

@utkarshpawade
Copy link
Contributor

@utkarshpawade utkarshpawade commented Mar 17, 2026

Fixes #492

Summary

Added 5 unit tests for previously untested edge cases in param_range(), param_glue(), and tidyselect_parameters():

  • param_range() / param_glue() return integer(0) when no parameters match
  • param_range() / param_glue() silently drop non-matching indices/names
  • tidyselect_parameters() works with negation (-alpha)

@codecov-commenter
Copy link

codecov-commenter commented Mar 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.66%. Comparing base (104facb) to head (1e64594).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #493   +/-   ##
=======================================
  Coverage   98.66%   98.66%           
=======================================
  Files          35       35           
  Lines        5857     5857           
=======================================
  Hits         5779     5779           
  Misses         78       78           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@jgabry jgabry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I would prefer to keep only a subset of these new tests that test previously untested behavior and drop the tests that only restate what test-helpers-mcmc.R already tests in

test_that("prepare_mcmc_array tidy parameter selection is same as traditional selection", {
pars_all <- c(
"(Intercept)", "beta[1]", "beta[2]", "sigma",
"b[(Intercept) XX:1]", "b[(Intercept) XX:2]", "b[(Intercept) XX:3]",
"b[(Intercept) ZZ:1]", "b[(Intercept) ZZ:2]", "b[(Intercept) ZZ:3]"
)
colnames(mat) <- pars_all
# check easier parameters
pars_char_1 <- c("(Intercept)", "beta[1]", "beta[2]", "sigma")
pars_tidy_1a <- vars(`(Intercept)`, `beta[1]`, `beta[2]`, sigma)
pars_tidy_1b <- vars(`(Intercept)`, contains("beta"), sigma)
pars_tidy_1c <- vars("(Intercept)", param_range("beta", 1:2), "sigma")
expect_identical(prepare_mcmc_array(mat, pars = pars_tidy_1a),
prepare_mcmc_array(mat, pars = pars_char_1))
expect_identical(prepare_mcmc_array(mat, pars = pars_tidy_1b),
prepare_mcmc_array(mat, pars = pars_char_1))
expect_identical(prepare_mcmc_array(mat, pars = pars_tidy_1c),
prepare_mcmc_array(mat, pars = pars_char_1))
# check multilevel parameters
pars_char_2 <- c("b[(Intercept) XX:1]", "b[(Intercept) ZZ:1]",
"b[(Intercept) XX:3]", "b[(Intercept) ZZ:3]")
pars_tidy_2a <- vars(param_glue("b[(Intercept) {var}:{lev}]",
var = c("XX", "ZZ"), lev = c(1, 3)))
expect_identical(prepare_mcmc_array(mat, pars = pars_tidy_2a),
prepare_mcmc_array(mat, pars = pars_char_2))
})
test_that("tidy parameter selection throws correct errors", {
expect_error(mcmc_hist(mat, pars = vars(contains("nonsense"))),
"No parameters were found matching those names")
expect_error(param_range("alpha", 1:3, vars = list("a", "b", "c")),
"'vars' must be NULL or a character vector.")
expect_error(param_glue("alpha[{lev}]", lev = 1:3, vars = 1:3,
"'vars' must be NULL or a character vector."))
})

Worth keeping:

  • direct param_range() no-match returns integer(0)
  • direct param_range() partial-match silently drops missing indices
  • direct param_glue() no-match returns integer(0)
  • direct param_glue() partial-match silently drops missing names
  • tidyselect_parameters() with negation

I think (although worth double checking) these are redundant with existing tests in test-helpers-mcmc.R and should be removed:

  • the invalid-vars error tests
  • the two prepare_mcmc_array(... vars(...)) integration tests
  • param_range selects correct parameters by index
  • param_glue selects correct parameters with one expression
  • param_glue selects correct parameters with multiple expressions
  • tidyselect_parameters selects by name
  • tidyselect_parameters works with tidyselect helpers
  • tidyselect_parameters works with contains()

@utkarshpawade
Copy link
Contributor Author

utkarshpawade commented Mar 18, 2026

thnx for the review, trimmed it down to just the 5 tests you mentioned: no-match, partial-match, and negation. Dropped everything already covered in test-helpers-mcmc.R

@jgabry jgabry merged commit d15d028 into stan-dev:master Mar 18, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add unit tests for tidy parameter selection helpers

3 participants